<?php
/**
 * Created by b17
 * 3/1/12 4:43 PM
 */

class PaymentController extends G4T_SSLController
{
    public function indexAction()
    {

    }

    public function doAction()
    {
        if ($this->_getParam('mode') == 'e') {
            $this->_forward('express');
        } else {
            $this->_forward('direct');
        }
    }

    public function expressAction()
    {
        $adapter = new \Managers\Payment\PayPal();
        try {
            $adapter->setExpressPayment($this->_getParam('money'));
        } catch (Exception $e) {
            $this->handleException($e);
            $this->_redirect('/user');
        }
    }

    public function completeAction()
    {
        $adapter = new \Managers\Payment\PayPal();
        try {
            $adapter->doExpressPayment($this->_user['id'], $this->_getAllParams());
            $this->_flashMessenger->addMessage('Счет успешно пополнен.', Lib_Flash::NOTICE, Lib_Flash::LATER);
        } catch (Exception $e) {
            $this->handleException($e);
        }
        $this->_redirect('/user');
    }

    public function cancelledAction()
    {
        $this->_flashMessenger->addMessage('Операция отменена.', Lib_Flash::WARNING, Lib_Flash::LATER);
        $this->_redirect('/user');
    }

    public function directAction()
    {
        $params = $this->_request->getParams();
        $form = new App_Form_Payment_Form();
        $form->populate($params);

        if($this->_request->isPost()) {
            if($form->isValid($params)) {
                $adapter = new \Managers\Payment\PayPal();
                try {
                    $adapter->doDirectPayment($this->_user['id'], $params);
                    $this->_flashMessenger->addMessage('Счет успешно пополнен.', Lib_Flash::NOTICE, Lib_Flash::PRE);
                    $this->_redirect('/user');
                } catch(Exception $e) {
                    $this->handleException($e);
                }
            } else {
                $this->_flashMessenger->addMessage('Please, check your input.', Lib_Flash::ERROR, Lib_Flash::POST);
            }
        }
        $this->view->form  = $form;
    }
}
